package com.tudouji.project.produce.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.project.bill.domain.Whoutformitem;
import com.tudouji.project.product.domain.Products;
import com.tudouji.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.produce.mapper.PickinglistitemMapper;
import com.tudouji.project.produce.domain.Pickinglistitem;
import com.tudouji.project.produce.service.IPickinglistitemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 领料单明细Service业务层处理
 * 
 * @author tudouji
 * @date 2022-06-01
 */
@Service
public class PickinglistitemServiceImpl implements IPickinglistitemService 
{
    @Autowired
    private PickinglistitemMapper pickinglistitemMapper;

    /**
     * 查询领料单明细
     * 
     * @param guid 领料单明细ID
     * @return 领料单明细
     */
    @Override
    public Pickinglistitem selectPickinglistitemById(String guid)
    {
        return pickinglistitemMapper.selectPickinglistitemById(guid);
    }

    /**
     * 查询领料单明细列表
     * 
     * @param pickinglistitem 领料单明细
     * @return 领料单明细
     */
    @Override
    public List<Pickinglistitem> selectPickinglistitemList(Pickinglistitem pickinglistitem)
    {
        return pickinglistitemMapper.selectPickinglistitemList(pickinglistitem);
    }

    /**
     * 新增领料单明细
     * 
     * @param pickinglistitem 领料单明细
     * @return 结果
     */
    @Override
    public int insertPickinglistitem(Pickinglistitem pickinglistitem)
    {
        return pickinglistitemMapper.insertPickinglistitem(pickinglistitem);
    }

    /**
     * 修改领料单明细
     * 
     * @param pickinglistitem 领料单明细
     * @return 结果
     */
    @Override
    public int updatePickinglistitem(Pickinglistitem pickinglistitem)
    {
        return pickinglistitemMapper.updatePickinglistitem(pickinglistitem);
    }

    /**
     * 批量删除领料单明细
     * 
     * @param guids 需要删除的领料单明细ID
     * @return 结果
     */
    @Override
    public int deletePickinglistitemByIds(String[] guids)
    {
        return pickinglistitemMapper.deletePickinglistitemByIds(guids);
    }

    /**
     * 删除领料单明细信息
     * 
     * @param guid 领料单明细ID
     * @return 结果
     */
    @Override
    public int deletePickinglistitemById(String guid)
    {
        return pickinglistitemMapper.deletePickinglistitemById(guid);
    }

    @Override

    /**
     * 根据领料单id,产品，更新重量 状态
     *
     * @param pcikguid
     * @param loadweight
     * @param price
     * @throws Exception
     */
    public BigDecimal updatePI(String pcikguid, String productid, String spguid, BigDecimal loadweight
            , BigDecimal price, String speguid){
        BigDecimal pricesum;
        pricesum = loadweight.multiply(price == null? new BigDecimal(0): price);
        Pickinglistitem item = new Pickinglistitem();
        item.setPickinglistid(pcikguid);
        item.setProductid(productid);
        item.setSthid(spguid);
//		item.setWeight(loadweight);
        item.setPrice(price);
        item.setPricesum(pricesum);
        item.setSpeid(speguid);
//		item.setOutweight(loadweight);
        pickinglistitemMapper.updateSI(item);
        return pricesum;
    }

    @Transactional
    public BigDecimal updatePIAfterWhout(Whoutformitem whoutformitem){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Pickinglistitem item = selectPickinglistitemById(whoutformitem.getItemguid());

        BigDecimal pricesum;
        pricesum = whoutformitem.getWeight().multiply(
                whoutformitem.getCostPrice() == null? new BigDecimal(0): whoutformitem.getCostPrice());

        item.setPricesum(pricesum);
        item.setPrice(whoutformitem.getCostPrice());
        item.setOutweight(whoutformitem.getWeight());
        item.setSthid(whoutformitem.getShrguid());
        item.setUpdateBy(sysUser.getUserId());

        pickinglistitemMapper.updatePickinglistitem(item);
        return pricesum;
    }

    @Override
    public List<Products> getProducts(Pickinglistitem product) {
        return pickinglistitemMapper.findProducts(product);
    }
}
